// components/liveroom/index.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    islogin: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selfid: app.globalData.openid,
    istest: true,
    info: undefined,
    pusher: new Object(),
    liverurls: [],
    liveplayers:[],
    openlive:"开启",
    islive:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    ///开始视频直播
    _login: function () {
      if(this.data.islive){
        this.setData({
          islive:false,
          openlive:"开启",
        });
       var pusher=  wx.createLivePusherContext(this);
       console.log(pusher);
        pusher.stop();
        this.triggerEvent('_exit', {
       
        })  // 将参数的形式传递给父组件
      }
      else{
        
         // 微信小程序中是通过triggerEvent来给父组件传递信息的
        this.triggerEvent('_login', {
       
         })  // 将参数的形式传递给父组件
    }
    },

    ///关闭视频直播
    _exit: function () {
      this.data.pusher.autopush = false;
      this.data.pusher.mode = "RTC";
      this.data.pusher.enablecamera = true;
      this.setData({
        pusher: this.data.pusher
      });
    },

    ///视频直播所有的人
    _list: function (list) {
       if(list  != '' &&  typeof list != 'object'){
          console.log(list);
        list = JSON.parse(list);
      }
        this.setData({
          selfid: app.globalData.openid,
          liverurls:list
        });
        console.log("list");
        console.log(this.data.selfid);
        var liveplayers = [];
        for(var index in this.data.liverurls){
          
            var liveplayersitem = new Object();
            liveplayersitem.url =  this.data.liverurls[index].url;
            liveplayersitem.id =  this.data.liverurls[index].openid;
            liveplayersitem.autoplay=true;
            liveplayersitem.mode= "RTC";
            liveplayersitem.muted = false;
            liveplayersitem.mincache = 0.2;
            liveplayersitem.maxcache=0.8;
            liveplayersitem.autopush=true;
            liveplayers.push(liveplayersitem);
        }
       
        this.setData({
          liveplayers:liveplayers
        });
        console.log("liveplayers");
        console.log(this.data.liveplayers);
    },
    bindchangeFn(option) {
      console.log(option)
      var type = option.currentTarget.dataset.cat;
      if (type == "beauty") {
        this.data.pusher.beauty = option.detail.value;

      } else if (type == "whiteness") {
        this.data.pusher.whiteness = option.detail.value;
      }
      this.setData({
        pusher: this.data.pusher
      });
    },

    openlive: function (livedata) {
      if(livedata  != '' &&  typeof livedata != 'object'){
        console.log(livedata);
        livedata = JSON.parse(livedata);
    }

      var pushurldata = new Object() ;
      for(var index in livedata){
        if(livedata[index].openid == app.globalData.openid){

          pushurldata.openid = livedata[index].openid;
          pushurldata.url = livedata[index].url;
          break;
        }
      }

      this.data.pusher.url = pushurldata.url;
      this.data.pusher.autopush = true;
      this.data.pusher.mode = "RTC";
      this.data.pusher.enablecamera = true;
      this.data.pusher.maxbitrate = 300;

      this.setData({
        pusher: this.data.pusher,
        islive:true,
        openlive:"关闭",
      });
      console.log(this.data.pusher);

      this._list(livedata);
    },
    bindnetstatus: function (e) {
      this.setData({
        info: e.detail.info
      });

    },
    statechange(e) {
      console.log('live-player code:', e.detail.code)
    },
    error(e) {
      console.error('live-player error:', e.detail.errMsg)
    },
    enablemic: function (e) {
      this.data.pusher.enablemic = e.detail.value;
      this.setData({
        pusher: this.data.pusher
      });
    },
  }
})